package Q110;

/**
 * 平衡二叉树
 *
 * @author 23737
 * @time 2021.11.26
 */
public class Test {
    public static void main(String[] args) {

    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        if(Math.abs(Depth(root.left)-Depth(root.right)) > 1){
            return false;
        }
        return isBalanced(root.left) && isBalanced(root.right);
    }

    private int Depth(TreeNode root) {
        if (root == null){
            return 0;
        }
        return Math.max(Depth(root.left),Depth(root.right))+1;
    }
}